/**
 * 
 */
package com.mths.client.page;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;
import com.mths.client.component.Button;
import com.mths.client.component.ComboBox;
import com.mths.client.component.GridData;
import com.mths.client.component.TabPanel;
import com.mths.client.component.TextField;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.DocumentController;
import com.mths.client.controller.DocumentControllerAsync;
import com.mths.client.dto.MetaParamDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ListItem;
import com.mths.client.util.PageUtil;

/**
 * @author ibesli
 * 
 */
public class DocumentMetaParamCreate extends AbstractPage implements IPage {

	private static DocumentCreateMetaUiBinder	uiBinder	= GWT.create(DocumentCreateMetaUiBinder.class);

	interface DocumentCreateMetaUiBinder extends UiBinder<Widget, DocumentMetaParamCreate> {
	}

	private final DocumentControllerAsync	documentAsync	= GWT.create(DocumentController.class);

	@UiField
	TabPanel								documentTabPnl;

	@UiField
	TextField								keyTxt, valueTxt;

	@UiField
	ComboBox								metaDataCmb;

	@UiField
	Button									saveBtn, clearBtn;

	@UiField
	GridData<MetaParamDTO>					paramTbl;

	private MetaParamDTO					dto				= new MetaParamDTO();

	public DocumentMetaParamCreate() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		initGridData();
		loadComponentData();
	}

	private void initGridData() {

		// metadata celltable
		paramTbl.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		TextColumn<MetaParamDTO> keyClm = new TextColumn<MetaParamDTO>() {

			@Override
			public String getValue(MetaParamDTO object) {
				return object.getKey();
			}
		};
		keyClm.setSortable(true);

		TextColumn<MetaParamDTO> valueClm = new TextColumn<MetaParamDTO>() {

			@Override
			public String getValue(MetaParamDTO object) {
				return object.getValue();
			}
		};

		paramTbl.addColumn(keyClm, "Meta Param Key");
		paramTbl.addColumn(valueClm, "Meta Param Value");
		paramTbl.setRowCount(10);

		final SingleSelectionModel<MetaParamDTO> selectionModelMetaData = new SingleSelectionModel<MetaParamDTO>();
		paramTbl.setSelectionModel(selectionModelMetaData);
		paramTbl.addDomHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				dto = selectionModelMetaData.getSelectedObject();
				prepareMetaData4Update();
			}

		}, DoubleClickEvent.getType());

	}

	private void initEventHandler() {
		documentTabPnl.selectTab(1);
		documentTabPnl.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				if (event.getSelectedItem() == 0) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_META_CREATE);
				} else if (event.getSelectedItem() == 1) {
					// loadMetaDataParams();
				} else if (event.getSelectedItem() == 2) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_CLASS_CREATE);
				} else if (event.getSelectedItem() == 3) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_TYPE_CREATE);
				} else if (event.getSelectedItem() == 4) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_MATCH_CREATE);
				}
			}
		});

		clearBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				refreshPage();
			}
		});

		saveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveOrUpdateMetaData();
			}
		});

		metaDataCmb.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				refreshPage();
				loadMetaParamList(metaDataCmb.getFieldValue());
			}

		});
	}

	private void loadMetaParamList(String metaOid) {
		documentAsync.loadMetaParamList(metaOid, new AsyncCall<List<MetaParamDTO>>() {

			@Override
			public void successCall(List<MetaParamDTO> result) {
				paramTbl.putDataList(result);
			}

		});
	}

	private void prepareMetaData4Update() {
		keyTxt.setFieldValue(dto.getKey());
		valueTxt.setFieldValue(dto.getValue());
	}

	private void loadComponentData() {
		loadMetaDataCmb();
	}

	private void loadMetaDataCmb() {
		documentAsync.loadMetaDataTypeCombo("", new AsyncCall<List<ListItem>>() {

			@Override
			public void successCall(List<ListItem> result) {
				metaDataCmb.addItemList(result);
			}

		});
	}

	private void saveOrUpdateMetaData() {
		dto.setKey(keyTxt.getFieldValue());
		dto.setValue(valueTxt.getFieldValue());
		dto.setMetaDataOid(metaDataCmb.getFieldValue());
		documentAsync.saveMetaParam(dto, new AsyncCall<Boolean>() {

			@Override
			public void successCall(Boolean result) {
				refreshPage();
				loadMetaParamList(metaDataCmb.getFieldValue());
			}

		});
	}

	private void refreshPage() {
		keyTxt.setFieldValue("");
		valueTxt.setFieldValue("");
		paramTbl.clear();
		dto = new MetaParamDTO();
	}

	@Override
	public String pageName() {
		return messages.DocumentMetaParamCreate_pageName();
	}
}
